﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataBusiness;
using Entity;

namespace UI.BasicData
{
    public partial class frmWellStructEdit : DevExpress.XtraEditors.XtraForm
    {
        tb_WellStructModel wellStructModel;
        //设置操作结果标识符
        public int returnFlag = 0;
        public frmWellStructEdit(tb_WellStructModel wellStructModel)
        {
            this.wellStructModel = wellStructModel;
            InitializeComponent();
            BindData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            this.WellNo.Text = wellStructModel.WellNo.ToString();
            this.CasingProgram.Text = wellStructModel.CasingProgram.ToString();
            this.HoleSize.Text = wellStructModel.HoleSize.ToString();
            this.TotalDepth.Text = wellStructModel.TotalDepth.ToString();
            this.WellSize.Text = wellStructModel.WellSize.ToString();
            this.StartLocation.Text = wellStructModel.StartLocation.ToString();
            this.EndLocation.Text = wellStructModel.EndLocation.ToString();
            this.TotalDepth.Text = wellStructModel.TotalDepth.ToString();
            this.CasingShoe.Text = wellStructModel.CasingShoe.ToString();
            this.CementDepth.Text = wellStructModel.CementDepth.ToString();
            this.Fpdfd.Text = wellStructModel.Fpdfd.ToString();
            this.Wdotnd.Text = wellStructModel.Wdotnd.ToString();
            this.Mdfdfnd.Text = wellStructModel.Mdfdfnd.ToString();
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbOK_Click(object sender, EventArgs e)
        {
            //检验是否填写完整
            if (CasingProgram.SelectedItem == null)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("套管程序您没有选择", "提示",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (CasingShoe.SelectedItem == null)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("套管鞋层位您没有选择", "提示",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (HoleSize.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("井眼尺寸您没有填写", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (TotalDepth.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("完钻深度您没有填写", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (WellSize.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("尺寸您没有填写", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (StartLocation.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("下入井段开始您没有填写", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (EndLocation.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("下入井段结束您没有填写", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (CementDepth.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("水泥返深您没有填写", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (this.Fpdfd.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("压裂压力钻井液密度您没有填写", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (this.Wdotnd.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("下次钻井目的井深您没有填写", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (this.Mdfdfnd.Text.Equals(""))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("下次钻井所用最高钻井液密度您没有填写", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            wellStructModel.WellNo = WellNo.Text.ToString();
            wellStructModel.CasingProgram = CasingProgram.Text.ToString();
            wellStructModel.HoleSize = string.IsNullOrEmpty(HoleSize.Text.ToString()) ? 0 : Convert.ToDecimal(HoleSize.Text.ToString());
            wellStructModel.TotalDepth = string.IsNullOrEmpty(TotalDepth.Text.ToString()) ? 0 : Convert.ToDecimal(TotalDepth.Text.ToString());
            wellStructModel.WellSize = string.IsNullOrEmpty(WellSize.Text.ToString()) ? 0 : Convert.ToDecimal(WellSize.Text.ToString());
            wellStructModel.StartLocation = string.IsNullOrEmpty(StartLocation.Text.ToString()) ? 0 : Convert.ToDecimal(StartLocation.Text.ToString());
            wellStructModel.EndLocation = string.IsNullOrEmpty(EndLocation.Text.ToString()) ? 0 : Convert.ToDecimal(EndLocation.Text.ToString());
            wellStructModel.CasingShoe = CasingShoe.Text.ToString();
            wellStructModel.CementDepth = string.IsNullOrEmpty(CementDepth.Text.ToString()) ? 0 : Convert.ToDecimal(CementDepth.Text.ToString());
            wellStructModel.UserID = Guid.NewGuid();
            wellStructModel.InputDate = DateTime.Now;
            wellStructModel.Fpdfd = string.IsNullOrEmpty(Fpdfd.Text.ToString()) ? 0 : Convert.ToDecimal(Fpdfd.Text.ToString());
            wellStructModel.Wdotnd = string.IsNullOrEmpty(Wdotnd.Text.ToString()) ? 0 : Convert.ToDecimal(Wdotnd.Text.ToString());
            wellStructModel.Mdfdfnd = string.IsNullOrEmpty(Mdfdfnd.Text.ToString()) ? 0 : Convert.ToDecimal(Mdfdfnd.Text.ToString());
            int result = WellStructBLL.UpdateWellStruct(wellStructModel);
            if (result >= 1)
            {
                returnFlag = 1;
                DialogResult = DevExpress.XtraEditors.XtraMessageBox.Show("您已经成功修改一条井身结构记录！", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (DialogResult == DialogResult.OK)
                {
                    this.Close();
                }
            }
            else
            {
                returnFlag = 0;
                DialogResult = DevExpress.XtraEditors.XtraMessageBox.Show("修改井身结构记录失败！", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (DialogResult == DialogResult.OK)
                {
                    this.Close();
                }
            }
        }

        /// <summary>
        /// 点击取消键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbCancel_Click(object sender, EventArgs e)
        {

            if (this.conFirmCancel() == DialogResult.OK)
            {
                this.Close();
            }
            else
            {
                return;
            }
        }

        //提醒用户是否取消添加记录的结果
        private DialogResult conFirmCancel()
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show("您确认取消吗？", "提示",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
        }

        private void WellStructAddItem_Load(object sender, EventArgs e)
        {
            //tbWellNum.Text = wellNum;
            WellNo.Enabled = false;
        }
    }
}